/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.effect
{
	
	import flash.events.Event;

	/**
	 * Dispatched when all paticles stop.
	 * @eventType Splash
	 */
	[Event(name="effect end",type="com.puny.mvc.views.effect.Splash")]
	/**
	 * Dispatched when one cycle of a particle ends.
	 * @eventType ParticleEvent
	 */
	[Event(name="end of cycle",type="com.puny.mvc.views.effect.events.ParticleEvent")]
	
	/**
	 * @private
	 * This class defines visual effect of a group of particles
	 * deprecated
	 */
	public class Splash extends IParticleField
	{
		public static const EFFECT_END:String = "Effect End";

		private var _density:Number = 2;
		private var _particles:Array = new Array();
		private var _index:int;
		private var _preGenerate:Boolean = true;

		private var _particle:Class;
		private var _asset:String;
		private var _effectEnd:Event = new Event(EFFECT_END);
		/**
		 * Constructor
		 * @param value the number of particles
		 * @param swf swf filename which includes the particle asset
		 * @param movieClip MovieClip name for the particle asset
		 * @param cls a class name which is inheriting <code>com.puny.mvc.views.effect.Particle</code>.
		 * @see com.puny.mvc.views.effect.Particle
		 */
		public function Splash(value:Number,swf:String,movieClip:String,cls:Class)
		{
			_density = value;
			setPerticle(cls, movieClip);
			super(swf);
//			embeded(new Swf());
		}

		/**
		 * Generate all particles before play is called if this is true
		 */
		protected function set preGenerate(value:Boolean):void {
			_preGenerate = value;
		}
		
		/**
		 * override to initialize particle.
		 * @return instance of particle
		 */
		protected function getParticle(particle:Particle):Particle {
			return particle;
		}

		private function addParticles():void {
			var count:int = _density;
			if (!_preGenerate) {
				count = _density * .05;
			}
			for(var i:int = 0;i < count;i++) {
				_particles.push(getParticle(new _particle(getSprite(_asset),this,0,0)));
			}
		}
		
		/**
		 * generate particles
		 */		
		override protected function setup():void {
			addParticles();
		}

		private function setPerticle(value:Class,name:String):void {
			_particle = value;
			_asset = name;
		}
		
		/**
		 * update particles in each frame.
		 */
		override protected function frameHandler(event:Event):void
		{
//			if (!_ready) {
//				return;
//			}
			if (stage!=null&&!_stopping&&!_preGenerate&&_particles.length<_density) {
				addParticles();
			}
			// go through the array of particles...
			var allStop:Boolean = true;
			for each(var particle:Particle in _particles)
			{
				if (particle.update()) {
					allStop = false;
				} else {
					if(particle.event!=null) {
						dispatchEvent(particle.event);
					}
					if (_stopping) {
						particle.stop();
					} else {
						allStop = false;
					}
				}
			}
			if (allStop) {
				trace("Splash Effect Ended");
				_particles.forEach(reset);
				removeEventListener(Event.ENTER_FRAME, frameHandler);
				while(!_preGenerate&&_particles.length > _density * .05) {
					_particles.pop();
				}
				dispatchEvent(_effectEnd.clone());
				complete();
			}
		}

		private function reset(obj:Particle,index:int,arr:Array):void {
			obj.moveable();
		}
		
		/**
		 * closing this class.
		 */
		override protected function beforeChildrenRemoved():void {
			trace("Splash Wrap up");
			removeEventListener(Event.ENTER_FRAME, frameHandler);
		}		
	}
}